﻿Public Class AddObjectVm
    Inherits KeyedViewModelBase

    Public Sub New()
        ObjectTypes = New RangeObservableCollection(Of ObjectType)
        ObjectSubTypes = New RangeObservableCollection(Of ObjectSubType)
        Sources = New RangeObservableCollection(Of RawSource)
    End Sub

    Public Sub New(library As RawLibrary, defaultObjectType As TokenLib.TypeCode)
        Me.New()

        Me.Library = library

        For Each item As ObjectType In [Enum].GetValues(GetType(ObjectType))
            If item <> ObjectType.ALL Then
                ObjectTypes.Add(item)
            End If
        Next

        For Each item As ObjectSubType In [Enum].GetValues(GetType(ObjectSubType))
            If item <> ObjectSubType.ALL Then
                ObjectSubTypes.Add(item)
            End If
        Next

        If defaultObjectType.ObjectType <> ObjectType.ALL Then
            SelectedObjectType = defaultObjectType.ObjectType
        End If
        If defaultObjectType.ObjectSubType <> ObjectSubType.ALL Then
            SelectedObjectSubType = defaultObjectType.ObjectSubType
        End If
    End Sub

    Public Sub New(library As RawLibrary, sourceObject As RawObject)
        Me.New(library, sourceObject.TypeCode)
        ObjectId = String.Format("CopyOf{0}", sourceObject.ObjectId)
        IsTypeLocked = True
    End Sub

    Private mAddCommand As ICommand = New Command(AddressOf AddCommand_Execute)
    Public ReadOnly Property AddCommand As ICommand
        Get
            Return mAddCommand
        End Get
    End Property
    Private Sub AddCommand_Execute(parameter As Object)
        Dim msg As New Messages.Common.CloseDialog(True)
        SendMessage(msg)
    End Sub

    Public Property ObjectTypes As RangeObservableCollection(Of ObjectType)
    Public Property ObjectSubTypes As RangeObservableCollection(Of ObjectSubType)
    Public Property Sources As RangeObservableCollection(Of RawSource)


    Private mSelectedObjectType As ObjectType
    Public Property SelectedObjectType As ObjectType
        Get
            Return mSelectedObjectType
        End Get
        Set(value As ObjectType)
            If mSelectedObjectType <> value Then
                mSelectedObjectType = value
                RaisePropertyChanged("SelectedObjectType")

                Dim prevSource As RawSource = SelectedSource
                Sources.Clear()
                Sources.AddRange(Library.Sources.Where(Function(s) s.ObjectType = mSelectedObjectType).OrderBy(Function(s) s.Path))
                If Sources.Count > 0 Then
                    If Not Sources.Contains(prevSource) Then
                        SelectedSource = Sources(0)
                    End If
                End If

                Dim descriptor As RawObjectDescriptor = mLibrary.GetDescriptorFromTypeName(SelectedObjectType.ToString)
                If descriptor IsNot Nothing Then
                    If Not descriptor.ObjectClasses.Contains(SelectedObjectSubType) Then
                        Dim temp As ObjectSubType? = descriptor.ObjectClasses.FirstOrDefault()
                        If temp.HasValue Then
                            SelectedObjectSubType = temp.Value
                        End If
                    End If
                End If
            End If
        End Set
    End Property

    Private mSelectedObjectSubType As ObjectSubType
    Public Property SelectedObjectSubType As ObjectSubType
        Get
            Return mSelectedObjectSubType
        End Get
        Set(value As ObjectSubType)
            If mSelectedObjectSubType <> value Then
                mSelectedObjectSubType = value
                RaisePropertyChanged("SelectedObjectSubType")
            End If
        End Set
    End Property

    Private mObjectId As String
    Public Property ObjectId As String
        Get
            Return mObjectId
        End Get
        Set(value As String)
            If mObjectId <> value Then
                mObjectId = value
                RaisePropertyChanged("ObjectId")
            End If
        End Set
    End Property

    Private mSource As TokenLib.RawSource
    Public Property SelectedSource As TokenLib.RawSource
        Get
            Return mSource
        End Get
        Set(value As TokenLib.RawSource)
            If mSource IsNot value Then
                mSource = value
                RaisePropertyChanged("SelectedSource")
            End If
        End Set
    End Property

    Private mLibrary As TokenLib.RawLibrary
    Public Property Library As TokenLib.RawLibrary
        Get
            Return mLibrary
        End Get
        Set(value As TokenLib.RawLibrary)
            If mLibrary IsNot value Then
                mLibrary = value
                RaisePropertyChanged("Library")
            End If
        End Set
    End Property

    Private mIsTypeLocked As Boolean
    Public Property IsTypeLocked As Boolean
        Get
            Return mIsTypeLocked
        End Get
        Set(ByVal Value As Boolean)
            If (mIsTypeLocked = Value) Then Return
            mIsTypeLocked = Value
            RaisePropertyChanged("IsTypeLocked")
        End Set
    End Property
    
End Class
